package edu.biwu.threadLocal;

public class ThreadLocalDemo {
    //创建一个ThreadLocal变量(对象),实现每个线程都有其单独的String变量(对象) 这样做 线程之间互不干扰
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        //创建Thread01线程,绑定自己的String对象
        Thread thread01 = new Thread(() -> {
            //相当于将"我是线程01的数据"和当前线程绑定
            threadLocal.set("我是线程01的数据");
            System.out.println(Thread.currentThread().getName()+"获取的数据:"+threadLocal.get());
        }, "thread-01");

        //创建Thread03线程,绑定自己的String对象
        Thread thread02 = new Thread(() -> {
            //相当于将"我是线程02的数据"和当前线程绑定
            threadLocal.set("我是线程02的数据");
            System.out.println(Thread.currentThread().getName()+"获取的数据:"+threadLocal.get());
        }, "thread-02");

        //启动线程
        thread01.start();
        thread02.start();
    }
}
